<?php
namespace app\home\controller;

use \think\Cache as Cache;
use \think\Validate as Validate;
use \think\Config as Config;
class Book extends Home{
    public function index(){
        $get = $this->req->get();
        $rule = [
            'page' => 'require'
        ];
        $validate = new Validate($rule);
        $model = new \app\home\model\Book;
        if($validate->check($get)){
            if($get['page'] == 1){
                $page = 0;
            }else{
                $page = $get['page'];
            }
        }else{
            $page = 0;
        }
        $rows = Config::get('paginate.list_rows');
        $list = $model->paginate($rows);
        $data = $model->limit($page*$rows,$rows)->select();
        $this->assign('list',$list);
        $this->assign('data',$data);
        return $this->fetch();
    }


    /**
     * 显示书本的所有章节；
     * 
     * @return html
     * @date Mon Jul  9 11:41:29 2018
     */
        
    public function detail(){
        $get = $this->req->get();
        $rule = [
            'id' => 'require',
            'name' => 'require'
        ];
        $validate = new Validate($rule);
        if($validate->check($get)){
            $model = new \app\home\model\BookChapter;
            $chapter = $model->where(['bk_id'=>$get['id']])->select();
            $this->assign('name',$get['name']);
            $this->assign('chapter',$chapter);
            return $this->fetch();
        } else {
            return $this->error('没有数据');
        }
    }

    /**
     * 阅读章节
     * 
     * @return html
     * @date Mon Jul  9 11:40:34 2018
     */
    public function read(){
        $get = $this->req->get();
        $rule = [
            'id' => 'require',
        ];
        $validate = new Validate($rule);
        if($validate->check($get)){
            $model = new \app\home\model\BookChapter;
            $chapter = $model->where(['chat_id'=>$get['id']])->find();
            $this->assign('chapter',$chapter);
            return $this->fetch();
        } else {
            return $this->error('没有数据');
        }
    }

    /**
     * 阅读章节
     * 
     * @return html
     * @date Mon Jul  9 11:40:34 2018
     */
    public function chapter(){
        $post = $this->req->post();
        $rule = [
            'id' => 'require',
        ];
        $validate = new Validate($rule);
        if($validate->check($post)){
            $model = new \app\home\model\BookChapter;
            $chapter = $model->where(['chat_id'=>$post['id']])->find();
            $this->assign('chapter',$chapter);
            $info = [
                'data' => $this->fetch(),
                'msg' => '获取成功',
            ];
            return $this->right($info);
        } else {
            $info = [
                'msg' => '获取数据失败',
            ];
            return $this->wrong($info);
        }
    }

}
